CXX = clang++
LD = clang++

# --- Include Path Configuration (from your original Makefile) ---
# This section attempts to get all system include directories from clang++
# and add them explicitly so clang tidy can find them. 
CXX_INCLUDE_DIRS := $(shell clang++ -E -x c++ - -v < /dev/null 2>&1 | grep -A 20 '#include <...>' | grep '^ ' | sed 's/^ //' | grep -v '(framework directory)')
CXX_INCLUDE_FLAGS := $(foreach dir,$(CXX_INCLUDE_DIRS),-isystem $(dir))
CXXFLAGS = -std=c++17 -I./include -Wunreachable-code -Wall -Wextra -Wno-error $(CXX_INCLUDE_FLAGS)

# --- Source and Object File Definitions ---
SRCDIR = src
# Place object files in the same directory as sources, or a separate build/obj directory
OBJDIR = $(SRCDIR)

# Automatically find all .cpp files in the source directory
SOURCES = $(wildcard $(SRCDIR)/*.cpp)

# Create a list of object file names based on sources, placing them in OBJDIR
# Example: src/main.cpp -> src/main.o
OBJECTS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SOURCES))

# --- Target Executables ---
TARGET_PROGRAM = program
TARGET_CLEAN_PROGRAM = clean_program # Assuming this is another program to be built from clean.cpp

# --- Specific Object Files (if needed for explicit dependencies) ---
# These should be correctly generated by the OBJECTS variable and pattern rule below.
# Listing them explicitly for clarity in target dependencies.
OBJ_MAIN = $(OBJDIR)/main.o
# Add other specific object files your 'program' executable depends on
OTHER_OBJS = $(OBJDIR)/helper.o $(OBJDIR)/types.o $(OBJDIR)/consumer.o $(OBJDIR)/another_consumer.o
OBJ_FOR_CLEAN_PROGRAM = $(OBJDIR)/clean.o


# --- Build Rules ---

# Default target: build all specified programs
all: $(TARGET_PROGRAM) $(TARGET_CLEAN_PROGRAM)

# Rule to link the main program
$(TARGET_PROGRAM): $(OBJ_MAIN) $(OTHER_OBJS)
	@echo "Linking $@..."
	$(LD) $^ -o $@ $(LDFLAGS)

# Rule to build and link the 'clean_program'
# This assumes clean.cpp is one of the files in $(SRCDIR)
$(TARGET_CLEAN_PROGRAM): $(OBJ_FOR_CLEAN_PROGRAM)
	@echo "Building and linking $@..."
	$(LD) $^ -o $@ $(LDFLAGS)

# --- Generic Pattern Rule for Compilation ---
# This rule tells Make how to build any .o file in OBJDIR from a .cpp file in SRCDIR.
# It crucially uses $(CXX) (clang++) and $(CXXFLAGS).
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
	@echo "Compiling $< to $@..."
	$(CXX) $(CXXFLAGS) -c $< -o $@

# --- Cleaning Rule ---
clean:
	@echo "Cleaning up object files and executables..."
	rm -f $(OBJDIR)/*.o $(TARGET_PROGRAM) $(TARGET_CLEAN_PROGRAM)

# --- Debugging Rule (optional) ---
debug:
	@echo "--- Debug Info ---"
	@echo "CXX: $(CXX)"
	@echo "CXXFLAGS: $(CXXFLAGS)"
	@echo "LDFLAGS: $(LDFLAGS)"
	@echo "SRCDIR: $(SRCDIR)"
	@echo "OBJDIR: $(OBJDIR)"
	@echo "SOURCES: $(SOURCES)"
	@echo "OBJECTS: $(OBJECTS)"
	@echo "TARGET_PROGRAM: $(TARGET_PROGRAM)"
	@echo "TARGET_CLEAN_PROGRAM: $(TARGET_CLEAN_PROGRAM)"
	@echo "--- End Debug Info ---"

# --- Phony Targets ---
# Declare targets that are not actual files
.PHONY: all clean debug